#!/bin/bash
# RETURN THE HOME DIRECTORY
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#TRANSLATE FROM PST INTO GMT
#USE THE DATE STRINGS PROVIDED
date2parse=${1}
	file2use=$(date -d "$date2parse" "+%m%d%Y" || echo "Format Not Recognized" && exit 1 )
	cl_date=$(date -d "$date2parse" "+%m%d%Y")
hour2parse=${2}
	
	hour2use=$(date -d "$hour2parse" "+%H:%M")
	hour2grep=" ${hour2use}:"
#TAKE rpt_name from calling routine
rprt_nm=${4}
#INCREMENT FUNCTION
incr()
{
	echo $(echo "$1 +1" | bc)  
}
#DECREMENT FUNCTION
decr()
{
	echo $(echo "$1 -1" | bc)  
}
#CLEAN ALL FLAGGING VARIABLES
reset()
{
	echo -n "0" > ${DIR}/.last_op
	echo -n "0" > ${DIR}/.last_state
	echo -n "0" > ${DIR}/.rec_time
	echo -n "0" > ${DIR}/.eq_time
	op_flag=0
	qc_flag=0
}
#ROUND FUNCTION
round()
{
	 python -c "print int(round($1))"
}
#CLEANUP
cleanup()
{
	rm ".${!}"
	reset
}

#GRAB GLOBAL VARIABLES
while read line; do
eval $line
done < Config/config
#GRAB DMS VARIABLES
while read line; do
eval $line
done < ${DIR}/dms.cfg
#THIS SCRIPT ACCEPTS DATE, TIME, and OUTPUT FORMAT AS PARAMETERS TO PARSE
use()
{
echo "You need 4 parameters passed. below find some examples..."
		echo "Be sure to give your queries in PST, as this will be translated to GMT as needed"
		echo "Use: $0 \"09/14/2014\" \"09:00\" \"DMSCL\" \"CO2_LDIR\" returns both DMS and CARBLogger format"
		echo "Use: $0 \"Sep 14, 2014\" \"13:00\" \"DMS\" \"Picarro\" returns DMS"
		echo "Use: $0 \"Sep 14, 2014\" \"1:00 PM\" \"CL\" \"Picarro\" returns carblogger"
		echo "Use: $0 \"September 14, 2014\" \"1:00 PM\" \"DMS\" \"Picarro\""


}

if [[ $# != "4" ]]; then
	{
	 use	
	}
fi

DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#REVIEW:
#DETERMINE THE DATE AND TIME WE SHOULD BEGIN INGESTING DATA
#SPECIFICATION:
#	C) RETURN THE FILE AND MINUTE TO INGEST
review() 
	{	
		
				#GRAB DMS VARIABLES
				while read line; do
				eval $line
				done < ${DIR}/dms.cfg
		
			hour2grep=`echo $hour2grep | sed 's/:$//g'`
			date -d @$(echo `date -d "$hour2grep $date2parse" +%s`" + 60 * 60 *${dms_hour_offset}" | bc) '+%H:%M: %Y-%m-%d'
	}
#SCRAPE:
#	A) ACCEPT A FILE $2 AND TIME $1 TO INGEST 
#	B) AVERAGE THE MINUTE
#	C) OUTPUT THE MINUTE FOR EACH PARAMETER IN CARBLOGGER TAGGED FORMAT ON A SINGLE LINE
#	EXAMPLE: CO2= CO2_DRY= CH4= CH4_DRY= H2O_VAPOR= VALVE
scrape()
{	
	(
	grep ${2} | grep " ${1}" | gawk '{ gsub(/;/," ")}; { sum3+=$3 ; sum4+=$4 ; sum5+=$5 ; sum6+=$6 ; sum7+=$7 ; sum8+=$8 ; sum9+=$9 ; sum10+=$10} END { print $1, $2, sum3/NR, sum4/NR, sum5/NR, sum6/NR, sum7/NR, sum8/NR, sum9/NR, sum10/NR, NR }; flush'
	)< ${DIR}/.write_cache	
}

# format_to_DMS:
#	A) TAKE THE LINES FROM SCRAPE(), AND CONVERT TO ONE MINUTE, PIPE DELIMITED FLAGGED OUTPUT
#	   BASED ON THE CAL RULES ACCEPTED FOR THE DRIVER
#	B) INPUTS INCLUDEs THE SCRAPE OUTPUT AND THE CAL EQUILIBRATTION AND RECOVERY PARAMETERS
#	AT THE TIME OF THIS WRITING THIS USES A ONE POINT CAL.
#		  
format_to_CL() 
{
	
	scrape `review`  | while read date_stamp time_stamp cav_pres cav_temp valve CO2 CO2_DRY CH4 CH4_DRY H2O data_count
		do
			#new_ts=$(echo "$cstamp1 - 28800" | bc) 
			#CREATE A CARBLOGGER FORMATTED TIME STAMP FOR GMT - 8
			#cstamp=$(date -d @${new_ts} '+%m-%d-%Y_%H:%M')
			ds=$(date "+%m-%d-%Y %H:%M")
			grp_strng=`review`
			dis_op=`awk '{ print $1+0 }'<${DIR}/.disabled1`
			if [ "$dis_op" -gt "0" ]; then
				{
						message="--DISABLED--"
				}
						else
				{
						message=''
				}
			fi
			carblogger_ds=$(date -d @`date -d "${hour2grep:0:6} $date2parse" +%s` '+%m-%d-%Y_%H:%M')
			cstamp="${rprt_nm}::${carblogger_ds} run_date_time=\"${ds}\" record_date=\"${grp_strng}\":::valve=${valve} CO2=${CO2} CO2_dry=${CO2_DRY} CH4=${CH4} CH4_DRY=${CH4_DRY} H2O=${H2O}  cav_pres=${cav_pres} cav_temp=${cav_temp} data_count=${data_count} ${message}"
			echo $cstamp
		done
}

format_to_DMS() 
{
	#GRAB GLOBAL VARIABLES
	while read line; do
	eval $line
	done < Config/config
	#GRAB DMS VARIABLES
	while read line; do
	eval $line
	done < ${DIR}/dms.cfg
	#AT THE TIME WRITTEN THE DMA MANUAL SPECIFIED THAT IT WOULD ACCEPT BOTH QC AND OP CODE
	#AFTER TRYING< IT WAS DETERMINED THAT IT DOES NOT. IGNOR QC_FLAG ASSIGNMENTS BELOW
	scrape `review` | while read date_stamp time_stamp cav_pres cav_temp valve "${CO2_STRING}" "${CO2_corr_STRING}" "${CH4_STRING}" "${CH4_corr_STRING}" "${H2O_STRING}" data_count 
		do
			op_flag=0
			qc_flag=0
			
			#(NON-TZ_CORRECTED) fmt_date=`date -d "$date_stamp $time_stamp" "+%d-%b-%Y %H:%M:00"`
			trnslt_tz=`date -d "$date_stamp $time_stamp" "+%s"`
			offset_tz=`echo "$trnslt_tz - ( ${dms_hour_offset} * 60 * 60 )" | bc`
			#echo " MY OFFSET IS" `echo "${dms_hour_offset} * 60 * 60" | bc`
			trns_date=`date -d@${offset_tz} "+%d-%b-%Y %H:%M:00"`
			#THIS VARIABLE STRICTLY FOR DETEMINING WHICH TIME SENSITIVE PRECISION CODE TO USE
			trns_dat_hr=`date -d@${offset_tz} "+%H"`
			#SET THE NUMERIC PRECISION CODE TO USE
			case ${trns_dat_hr} in
				00|01|02|03|04|05 )
					prec_code=2
					;;
				06|07|08|09|10|11 )
					prec_code=12
					;;
				12|13|14|15|16|17 )
					prec_code=22
					;;
				18|19|20|21|22|23 )
					prec_code=32
					;;
			esac
			#Determine is adequate subminute data is available
			if [ `round ${data_count}` -ge "45" ]; then
				{
					#DEBUG
					rec_time=`awk '{ print $1+0 }'<${DIR}/.rec_time`
					eq_time=`awk '{ print $1+0 }'<${DIR}/.eq_time`
					last_op=`awk '{ print $1+0 }'<${DIR}/.last_op`
					dis_op=`awk '{ print $1+0 }'<${DIR}/.disabled1`
					#FLAG FOR NOT IN RECOV PHASE, SAMPLING AMBIENT DATA
					if [ `round ${valve}` -eq "$samp_state" ] && [ "$rec_time" -le "0" ] && [ "$eq_time" -le "0" ] && [ "$last_op" -le "0" ]; then
					{ 
						#FLAG FOR CHANNEL DISABLED
						#NOTE WE DO NOT SET LAST OP FLAG BECAUSE THIS SHOULD BE EXEMPT FROM
						#CALIBRATIONS
						
						if [ "$dis_op" -ne "0" ]; then
						{
							op_flag=9
						}
						else
						{
							op_flag=0
						}
						fi
						qc_flag=0
						#DEBUG
						
						for i in "${CO2_STRING}" "${CO2_corr_STRING}" "${CH4_STRING}" "${CH4_corr_STRING}" "${H2O_STRING}"
						#$CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						do
							echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done
						last_state=0
						echo -n "$rec_time" > ${DIR}/.rec_time
						echo -n "$op_flag" > ${DIR}/.last_state
					}
					#FLAG FOR NOT IN RECOV PHASE, FIRST 1 MINUTES OF SAMPLING PRECISION GAS UP TO EQ TIME
					elif [ `round ${valve}` -eq "$prec_state" ] && [ "$rec_time" -le "0" ] && [ "$eq_time" -le "0" ] && [ "$last_op" -eq "0" ]; then
					{
						op_flag=9
						qc_flag=69
						#DEBUG
						echo -n "$op_flag" > ${DIR}/.last_op
						echo -n "${def_eq_time}" > ${DIR}/.eq_time
					#	echo "VALVE AND RECOV == $eq_time $recov_time"
						for i in $CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						#for i in CO2 CO2_corr CH4 CH4_corr H2O
						do
							echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done
					#	#Determine if the cal valve was on
					}
					#FLAG FOR NOT IN RECOV PHASE, DURING EQ TIME SAMPLING PRECISION GAS
					elif [ `round ${valve}` -eq "$prec_state" ] && [ "$rec_time" -le "0" ] && [ "$eq_time" -gt "0" ] && [ "$last_op" -ne "0" ]; then
					{
						op_flag=9
						qc_flag=69
						#DEBUG
						echo -n "9" > ${DIR}/.last_op
						new_eq_time=`echo "$eq_time - 1" | bc`
						echo -n ${new_eq_time} > ${DIR}/.eq_time
					#	echo "VALVE AND RECOV == $eq_time $recov_time"
						for i in $CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						#for i in CO2 CO2_corr CH4 CH4_corr H2O
						do
							echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done
						#Determine if the cal valve was on
					}
					#FLAG FOR PRECISION PHASE,  AFTER EQ TIME SAMPLING PRECISION GAS
					elif [ `round ${valve}` -eq "$prec_state" ] && [ "$rec_time" -le "0" ] && [ "$eq_time" -le "0" ] && [ "$last_op" -ne "0" ]; then
					{
						if [ "$dis_op" -ne "0" ]; then
						{
							op_flag=9
						}
						else
						{
							op_flag=${prec_code}
						}
						fi
						
						
						
						qc_flag=0
						#DEBUG
						echo -n "2" > ${DIR}/.last_op
						for i in $CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						#for i in CO2 CO2_corr CH4 CH4_corr H2O
						do
							echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done
						#Determine if the cal valve was on
					}
					#FLAG FOR INITIALIZING RECOVERY PHASE
					elif [ `round ${valve}` -eq "${samp_state}" ] && [ "${rec_time}" -le "0" ] && [ "${eq_time}" -le "0" ] && [ "${last_op}" -eq "2" ]; then
					{
						op_flag=9
						qc_flag=69
						#DEBUG
						echo -n "$op_flag" > ${DIR}/.last_op
						echo -n "$def_rec_time" > ${DIR}/.rec_time
						#echo "VALVE AND RECOV == $eq_time $recov_time"
						for i in $CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						#for i in CO2 CO2_corr CH4 CH4_corr H2O
						do
							echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done
						#Determine if the cal valve was on
					}
					#FLAG FOR FINISHING RECOVERY PHASE
					elif [ `round ${valve}` -eq "$samp_state" ] && [ "${rec_time}" -gt "0" ] && [ "$eq_time" -le "0" ] && [ "$last_op" -eq "9" ]; then
					{
						op_flag=9
						qc_flag=69
					#	#DEBUG
						echo -n "9" > ${DIR}/.last_op
						new_rec_time=`echo "$rec_time - 1" | bc`
						echo -n ${new_rec_time} > ${DIR}/.rec_time
						for i in $CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						#for i in CO2 CO2_corr CH4 CH4_corr H2O
						do
							echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done
						#Determine if the cal valve was on
					}
					#FLAG FOR RESUMING NORMAL SAMPLING
					elif [ `round ${valve}` -eq "${samp_state}" ] && [ "${rec_time}" -le "0" ] && [ "$eq_time" -le "0" ] && [ "$last_op" -eq "9" ]; then
					{
						
						if [ "$dis_op" -ne "0" ]; then
						{
							op_flag=9
						}
						else
						{
							op_flag=0
						}
						fi
						
						qc_flag=0
						echo -n "0" > ${DIR}/.last_op
						new_rec_time=`echo "1 - 1" | bc`
						echo -n ${new_rec_time} > ${DIR}/.rec_time
						for i in $CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						#for i in CO2 CO2_corr CH4 CH4_corr H2O
						do
							echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done
						#Determine if the cal valve was on
					}
					fi
					
				}
			else
				{
						
						dis_op=`awk '{ print $1+0 }'<${DIR}/.disabled1`	
						if [ "$dis_op" -ne "0" ]; then
						{
							op_flag=9
						}
						else
						{
							op_flag=8
						}
						fi
						
						qc_flag=7

						for i in $CO2_STRING $CO2_corr_STRING $CH4_STRING $CH4_corr_STRING $H2O_STRING
						#for i in CO2 CO2_corr CH4 CH4_corr H2O
						do
						#date -d "$date2parse" "+%m%d%Y"	
						echo "${saroad}|$min_${i}|${trns_date}|${op_flag}|${!i}|"
						done	
				}
			fi
		
		done
}
case $3 in
	CL*)
	format_to_CL
	;;
	DMS*)
	#review
	#scrape `review`
	format_to_DMS
	;;
	DMSCL)
	format_to_DMS
	format_to_CL
	;;
	*)
	use
	;;

esac
#echo "STATE VARIABLES"
#cat resources/Picarro_drv/.write_cache | grep " 07:59" | grep "2014-11-03" | gawk '{ gsub(/;/," ")}; { sum3+=$3 ; sum4+=$4 ; sum5+=$5 ; sum6+=$6 ; sum7+=$7 ; sum8+=$8 ; sum9+=$9 ; sum10+=$10} END { print $1, $2, sum3/NR, sum4/NR, sum5/NR, sum6/NR, sum7/NR, sum8/NR, sum9/NR, sum10/NR, NR }; flush'
#echo "present state: recovery time variable:"`cat ${DIR}/.rec_time`
#echo "present state: eq time variable:"`cat ${DIR}/.eq_time`
#echo "present state: sample state variable:"${samp_state}
#echo "present state: last state variable:"`cat ${DIR}/.last_op`
#echo "SYSTEM VARIABLES"
#echo "present state: eq time:${def_eq_time}"
#echo "present state: rec time:${def_rec_time}"
#echo "present state: prec state:${prec_state}"
#echo "present state: samp state:${samp_state}"
#scrape `review`
#echo "${CO2_STRING} ${CO2_corr_STRING} ${CH4_STRING} ${CH4_corr_STRING} ${H2O_STRING}"
#Le DeBugs-oise
#incr 1
#decr 2
#round '.6'
#round '.3'